package org.jeecg.modules.demo.zhibiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.dto.DaibanDto;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.entity.Jieshouzhibiaodaibanbiao;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.mapper.JieshouzhibiaodaibanbiaoMapper;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.service.IJieshouzhibiaodaibanbiaoService;
import org.jeecg.modules.demo.zhibiao.entity.Zhibiaobiao;
import org.jeecg.modules.demo.zhibiao.mapper.ZhibiaobiaoMapper;
import org.jeecg.modules.demo.zhibiao.service.IZhibiaobiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 指标表
 * @Author: jeecg-boot
 * @Date:   2025-09-04
 * @Version: V1.0
 */
@Service
public class ZhibiaobiaoServiceImpl extends ServiceImpl<ZhibiaobiaoMapper, Zhibiaobiao> implements IZhibiaobiaoService {

    @Autowired
    private IJieshouzhibiaodaibanbiaoService jieshouzhibiaodaibanbiaoService;
    //根据登录用户自动添加责任部门
    @Override
    public Zhibiaobiao addZhiBiao(Zhibiaobiao zhibiaobiao) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        zhibiaobiao.setZerenbumen(loginUser.getOrgId());
        this.save(zhibiaobiao);
        //将指标id和配合部门id插入代办表
        //获取配合部门id依次遍历插入数据
        //创建一个list用来接收配合部门id
        String peiheId = zhibiaobiao.getPeihebumen();
        String[] peiheIds = peiheId.split(",");
        for(int i =0;i<peiheIds.length;i++){
            Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao= new Jieshouzhibiaodaibanbiao();
            jieshouzhibiaodaibanbiao.setPeihebumen(peiheIds[i]);
            jieshouzhibiaodaibanbiao.setZhibiaoid(zhibiaobiao.getId());
            jieshouzhibiaodaibanbiao.setZhuangtai("待处理");
            jieshouzhibiaodaibanbiaoService.save(jieshouzhibiaodaibanbiao);
        }
        return zhibiaobiao;
    }
}
